package com.eling.elcms.travel.service.impl;

import com.eling.elcms.travel.dao.IDailyBusinessDao;
import com.eling.elcms.travel.model.NonMemberGroup;
import com.eling.elcms.travel.model.NonMemberGroup.TourType;
import com.eling.elcms.travel.model.view.DailyBusiness;
import com.eling.elcms.travel.service.IDailyBusinessService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DailyBusinessServiceImpl  implements IDailyBusinessService{
	
	@Autowired
	private IDailyBusinessDao dao;


	@Override
	public Map<String, DailyBusiness> query(Long pkServicePoint, Date start, Date end) {
		Map<String, DailyBusiness> map = new HashMap<>();
		//会员团
		map.put("memberGroup", alliance(pkServicePoint,start,end,true));
		//联盟团
		map.put("assGroup", alliance(pkServicePoint,start,end,false));
		//会员散客
		map.put("memSingle", single(pkServicePoint,start,end,NonMemberGroup.TourType.IndivMember));
		//旅游团队
		map.put("travelGroup", single(pkServicePoint,start,end,NonMemberGroup.TourType.TravelGroup));
		//旅游散客
		map.put("travelSingle", single(pkServicePoint,start,end,NonMemberGroup.TourType.Individual));
		//其他
	//	map.put("other", single(pkServicePoint,start,end,NonMemberGroup.TourType.Individual));
		//全部
		map.put("all", countAll(map));
		return map;
	}

	private DailyBusiness countAll(Map<String, DailyBusiness> map) {
		DailyBusiness all = new DailyBusiness();
		for (DailyBusiness business : map.values()) {
			all.setDayAccountsReceivable(all.getDayAccountsReceivable()==null?0D:all.getDayAccountsReceivable()+business.getDayAccountsReceivable());
			all.setDayAccountsReceived(all.getDayAccountsReceived()==null?0D:all.getDayAccountsReceived()+business.getDayAccountsReceived());
			all.setMonthAccountsReceivable(all.getMonthAccountsReceivable()==null?0D:all.getMonthAccountsReceivable()+business.getMonthAccountsReceivable());
			all.setMonthAccountsReceived(all.getMonthAccountsReceived()==null?0D:all.getMonthAccountsReceived()+business.getMonthAccountsReceived());
			all.setMonthAccountsPlan(all.getMonthAccountsPlan()==null?0D:all.getMonthAccountsPlan()+business.getMonthAccountsPlan());
			all.setYearAccountsReceivable(all.getYearAccountsReceivable()==null?0D:all.getYearAccountsReceivable()+business.getYearAccountsReceivable());
			all.setYearAccountsReceived(all.getYearAccountsReceived()==null?0D:all.getYearAccountsReceived()+business.getYearAccountsReceived());
		}
		return all;
	}

	//散客
	private DailyBusiness single(Long pkServicePoint, Date start, Date end, TourType tourType) {
		DailyBusiness single = new DailyBusiness();
		single.setDayAccountsReceivable(dao.singleReceivable(pkServicePoint,start,end,tourType));
		single.setDayAccountsReceived(dao.singleReceived(pkServicePoint,start,end,tourType));
		
		
		Date monthStart =DateUtils.ceiling(start, Calendar.MONTH);
		single.setMonthAccountsReceivable(dao.singleReceivable(pkServicePoint,monthStart,end,tourType));
		single.setMonthAccountsReceived(dao.singleReceived(pkServicePoint,monthStart,end,tourType));
		single.setMonthAccountsPlan(dao.singleReceivable(pkServicePoint,monthStart,DateUtils.addMonths(monthStart, 1),tourType)+dao.singleAppoint(pkServicePoint, monthStart, DateUtils.addMonths(monthStart, 1), tourType));
		
		
		Date yearStart =DateUtils.ceiling(start, Calendar.YEAR);
		single.setYearAccountsReceivable(dao.singleReceivable(pkServicePoint,yearStart,end,tourType));
		single.setYearAccountsReceived(dao.singleReceived(pkServicePoint,yearStart,end,tourType));
		
		return single;
	}

	//团
	private DailyBusiness alliance(Long pkServicePoint, Date start,Date end,boolean alliance) {
		DailyBusiness ass = new DailyBusiness();
		ass.setDayAccountsReceivable(dao.assReceivable(pkServicePoint,start,end,alliance));
		ass.setDayAccountsReceived(dao.assReceived(pkServicePoint,start,end,alliance));
		
		
		Date monthStart =DateUtils.ceiling(start, Calendar.MONTH);
		ass.setMonthAccountsReceivable(dao.assReceivable(pkServicePoint,monthStart,DateUtils.addMonths(monthStart, 1),alliance));
		ass.setMonthAccountsReceived(dao.assReceived(pkServicePoint,monthStart,DateUtils.addMonths(monthStart, 1),alliance));
		ass.setMonthAccountsPlan(ass.getMonthAccountsReceivable()+dao.assAppoint(pkServicePoint, monthStart, DateUtils.addMonths(monthStart, 1), alliance));
		
		
		Date yearStart =DateUtils.ceiling(start, Calendar.YEAR);
		ass.setYearAccountsReceivable(dao.assReceivable(pkServicePoint,yearStart,DateUtils.addYears(yearStart, 1),alliance));
		ass.setYearAccountsReceived(dao.assReceived(pkServicePoint,yearStart,DateUtils.addYears(yearStart, 1),alliance));
		
		return ass;
	}

}
